<?php
/**
 *	KLF Framework
 *
 *	@author Kristian Oye <kristianoye@gmail.com> 
 *	@copyright (C) 2010-2011, Kristian Oye
 */
namespace KLF\System\Web
{
	use KLF\System\KPropertyContext;

	use KLF\System\KObject;
	use KLF\System\Collections\Specialized\KNameValueCollection;
	
	/**
	 *  Class KHttpResponse
	 *  
	 *  @package KLF\System\Web
	 *  @access public
	 *  @author Kristian Oye <kristianoye@gmail.com>
	 *  @since 0.1
	 *  
	 *  @method KHttpResponse NewInstance
	 *  
	 *  @property boolean $BufferOutput Indicates whether buffering is on (true) or off (false).
	 *  @property string $ContentType The MIME type of the HTTP response.
	 *  @property KHttpCookieCollection $Cookies The cookies to be sent to the client.
	 *  @property integer $Expires The maximum age of the cache.
	 *  @property integer $ExpiresAbsolute The cache must absolutely be deleted.
	 *  @property string $RedirectionLocation The URL to redirect the client to.
	 *  @property KNameValueCollection $Headers A list of additional headers to add to the response.
	 *
	 */
	class KHttpResponse extends KObject
	{
		/**
		 * Returns true if buffering is on.  False if it is off.
		 * 
		 * @var boolean $_propBufferOutput
		 */
		protected $_propBufferOutput = false;
		
		/**
		 * Contains the MIME type of the response.
		 * 
		 * @var string $_propContentType
		 */
		protected $_propContentType = "text/html";
		
		/**
		 * The cookies to be sent to the client
		 * 
		 * @var KHttpCookieCollection $_propCookies
		 */
		protected $_rpropCookies;
		
		/**
		 * The time which the cache should be allowed to live.
		 * 
		 * @var integer $_propExpires
		 */
		protected $_propExpires=-1;
		
		/**
		 * The aging of the cache is absolute.
		 * 
		 * @var boolean $_rpropExpiresAbsolute
		 */
		protected $_rpropExpiresAbsolute = null;
		
		/**
		 * Headers to be added to the response.
		 * 
		 * @var KNameValueCollection $_propHeaders A list of HTTP headers to send in the response.
		 */
		protected $_rpropHeaders;
				
		/**
		 * The URL that the client should be redirected to.
		 * 
		 * @var string $_propRedirectLocation
		 */
		protected $_propRedirectLocation = null;
		
		/**
		 * The status code of the response.
		 * 
		 * @var KHttpStatus $Status
		 */
		protected $_propStatus;
		
		/**
		 * Construct a new response object.
		 */
		public function __construct()
		{
			$this->_propStatus = KHttpStatus::createStatus(KHttpStatus::HTTP_STATUS_OK);
			$this->_rpropHeaders = new KNameValueCollection();
		}

		/**
		 * Toggle buffering on and off.
		 * 
		 * @param boolean $val
		 * @param integer $pc
		 */
		protected function propBufferOutput($val, $pc)
		{
			if ($pc === KPropertyContext::PROP_GET)
			{
				return $this->_propBufferOutput;
			}
			elseif($pc === KPropertyContext::PROP_SET)
			{
				$this->_propBufferOutput = $val;
				
				if ($val === true)
				{
					ob_start();
				}
				else 
				{
					echo ob_get_clean();
				}
				return $this->_propBufferOutput;
			}
		}
	}
}
